import sys
import math
import typing
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from Smoke import Smoke
from SmokeSet import SmokeSet


class BigEnemy(QGraphicsItem):
    def __init__(self, scene):
        super(BigEnemy, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()

        self.isshowblood = 0
        self.blood = 5000
        self.current_blood = 5000
        self.current_blood_front_x = 79
        self.blood_front_x = 79
        self.blood_gb = QPixmap(self.root + "/res/doom_slider_back.png").scaled(81, 14)  # 血条背景
        self.blood_front = QPixmap(self.root + "/res/doom_slider_front.png").scaled(self.current_blood_front_x,
                                                                                    10)  # 血条前景

        self.setVisible(False)  # 设为不可视
        self.speed = 1  # 设置速度
        self.Rotate = 0
        self.StartRotate = 0
        self.pix = []
        for i in range(6):
            self.pix.append(QPixmap(self.root + "/res/big_bad_frame" + str(i + 1) + ".png"))
        # self.pix = QPixmap(self.root + "/res/big_bad_frame1.png")

        self.state = 0

        self.stopTime = 0
        self.scene = scene
        self.scene.addItem(self)
        #self.smoke = Smoke(self.scene)

        self.MuchSet = 0  # 表示第几集合

    def setSet(self, MuchSet):
        self.MuchSet = MuchSet

    def boundingRect(self):
        return QRectF(-self.pix[0].width() * 0.5, -self.pix[0].height() * 0.5, self.pix[0].width(),
                      self.pix[0].height() - 16)

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):
        if self.isshowblood == 1:
            painter.drawPixmap(-self.blood_gb.width() * 0.5, -self.pix[0].height() * 0.5 - 14, self.blood_gb)
            painter.drawPixmap(-self.blood_gb.width() * 0.5 + 1, -self.pix[0].height() * 0.5 - 12,
                               self.blood_front.scaled(self.current_blood_front_x, 10))
        if -90 > self.Rotate > -270:
            painter.drawPixmap(-self.pix[self.state + 3].width() * 0.5, -self.pix[self.state + 3].height() * 0.5,
                               self.pix[self.state + 3])
        else:
            painter.drawPixmap(-self.pix[self.state].width() * 0.5, -self.pix[self.state].height() * 0.5,
                               self.pix[self.state])
        if self.stopTime % 40 == 0:
            self.state = (self.state + 1) % 3

    def setBlood(self, damage):
        self.isshowblood = 1
        self.current_blood -= 200
        a = damage / self.blood
        self.current_blood_front_x -= self.blood_front_x * a
        self.update()

    def CallSmoke(self):
        pass
        self.smokeSet = SmokeSet(self.scene)
        self.smokeSet.CallSmokeSet(StartX = self.mapToScene(0,0).x(),StartY = self.mapToScene(0,0).y())
        self.smokeSet.CallEnemyPart(self.mapToScene(0,0).x(),self.mapToScene(0,0).y(),self.Rotate)

        # self.smoke.CallSmoke(self.mapToScene(0, 0).x(), self.mapToScene(0, 0).y())
        # self.smoke.CallSpareParts(self.mapToScene(0, 0).x(), self.mapToScene(0, 0).y(), self.Rotate)

    def advance(self, phase: int):
        self.stopTime = (self.stopTime + 1) % 21
        if self.stopTime % 20 == 0:
            if self.isVisible():
                if self.mapToScene(0, 0).x() < -150 or self.mapToScene(0, 0).y() < -150 or \
                        self.mapToScene(0, 0).x() > self.scene.width() + 150 or self.mapToScene(0,
                                                                                                0).y() > self.scene.height() + 150:
                    self.setVisible(False)

                    self.isshowblood = 0
                    self.current_blood = self.blood
                    self.current_blood_front_x = self.blood_front_x
                else:
                    self.setStartRotate(self.StartRotate)
                    self.setPos(self.mapToScene(self.speed, 0))
                    self.setRotation(self.Rotate)  # 出错地方,一直都是StartRotate的角度

    def setPos(self, pos: typing.Union[QPointF, QPoint]):
        super(BigEnemy, self).setPos(pos)

    def setRotation(self, angle: float):
        self.Rotate = angle
        if -90 > self.Rotate > -270:
            super(BigEnemy, self).setRotation(180 + angle)
        else:
            super(BigEnemy, self).setRotation(angle)

    def setStartRotate(self, angle: float):
        self.StartRotate = angle
        super(BigEnemy, self).setRotation(angle)

    def setVisible(self, visible: bool):
        super(BigEnemy, self).setVisible(visible)

    def isVisible(self):
        return super(BigEnemy, self).isVisible()
